<%--
Created by IntelliJ IDEA.
User: 陪你度过漫长岁月
Date: 2022/8/5
Time: 11:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增商品</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<body>
<form class="layui-form" method="post">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-block">
                    <input type="text" name="miid" id="miid" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <input type="text" name="username" id="username" lay-verify="title" autocomplete="off"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" id="email" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="text" name="password" id="password" lay-verify="title" autocomplete="off"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" id="phone" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" id="nan" checked value="男" title="男">
                    <input type="radio" name="sex" value="女" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">米昵称</label>
                <div class="layui-input-block">
                    <input type="text" name="miname" id="miname" lay-verify="title" autocomplete="off"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户身份</label>
                <div class="layui-input-block">
                    <input type="text" name="manager" id="manager" lay-verify="title" autocomplete="off"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-block">
                    <input type="text" name="address" id="address" lay-verify="title" autocomplete="off"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    $("form").submit(function () {
        let bool = false
        var sex;
        if ($('#nan').is(':checked')) {
            sex = "男"
        } else {
            sex = "女"
        }
        let datas = "";
        var email = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/ //邮箱正则表达式
        if ($("#miid").val() == "" || $("#username").val() == "" || $("#email").val() == "" || $("#password").val() == "" || $("#phone").val() == "" || $("#miname").val() == "" || $("#manager").val() == "" || $("#address").val() == "") {
            layer.msg('所有信息均不能为空!', {
                icon: 2,
                time: 2000,//2秒关闭（如果不配置，默认是3秒）
                anim: 6,
                top: 5
            });
            return false
        } else if (isNaN($("#miid").val())) {
            layer.msg('米ID的类型格式不匹配!', {
                icon: 2,
                time: 2000,//2秒关闭（如果不配置，默认是3秒）
                anim: 6,
                top: 5
            });
            return false
        } else if (!email.test($("#email").val())) {
            layer.msg('请输入正确的邮箱格式!', {
                icon: 2,
                time: 2000,//2秒关闭（如果不配置，默认是3秒）
                anim: 6,
                top: 5
            });
        } else if ($("#phone").val().length != 11) {
            layer.msg('请输入正确的手机号!', {
                icon: 2,
                time: 2000,//2秒关闭（如果不配置，默认是3秒）
                anim: 6,
                top: 5
            });
            return false
        }
        $.ajaxSettings.async = false
        $.post("${pageContext.request.contextPath}/AddUserServlet",
            {
                "miid": $("#miid").val(),
                "username": $("#username").val(),
                "email": $("#email").val(),
                "password": $("#password").val(),
                "phone": $("#phone").val(),
                "sex": sex,
                "miname": $("#miname").val(),
                "manager": $("#manager").val(),
                "address": $("#address").val()
            }, function (data) {
                console.log(data)
                datas = data;
                if (data == "true") {
                    parent.layui.table.reload('user-table', {page: {curr: 1}});
                    parent.layer.msg("添加成功!")
                    parent.layer.close(parent.layer.getFrameIndex(window
                        .name)); //关闭当前页
                } else {
                    bool = true
                    layer.msg('请检查参数是否正确!', {
                        icon: 2,
                        time: 2000,//2秒关闭（如果不配置，默认是3秒）
                        anim: 6,
                        top: 5
                    });
                }
            });
        if (bool) {
            return false
        }

    })

    <%--function su() {--%>
    <%--    $.ajax({--%>
    <%--        url: '${pageContext.request.contextPath}/AddUserServlet',--%>
    <%--        data: {"username": $("#username").val()},--%>
    <%--        dataType: 'json',--%>
    <%--        contentType: 'application/json',--%>
    <%--        type: 'post',--%>
    <%--        success: function (result) {--%>
    <%--            if (result.success) {--%>
    <%--                layer.msg(result.msg, {--%>
    <%--                    icon: 1,--%>
    <%--                    time: 1000--%>
    <%--                }, function () {--%>
    <%--                    parent.layui.table.reload("user-table");--%>
    <%--                });--%>
    <%--            } else {--%>
    <%--                layer.msg(result.msg, {--%>
    <%--                    icon: 2,--%>
    <%--                    time: 1000--%>
    <%--                });--%>
    <%--            }--%>
    <%--        }--%>
    <%--    })--%>
    <%--}--%>
</script>
</body>
</html>
